package com.css.dzj.dzjap.modules.apunit.controller;

import com.alibaba.fastjson.JSON;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.audit.service.SysAuditService;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.HttpClientUtil;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.common.util.userinfo.UserInfoService;
import com.css.dzj.dzjap.modules.apunit.entity.ApUnitEntity;
import com.css.dzj.dzjap.modules.apunit.service.ApunitService;
import com.css.dzj.dzjap.modules.enterprise.entity.ApEnterpriseCreditEntity;
import com.css.dzj.dzjap.modules.enterprise.service.EnterpriseService;
import com.css.dzj.dzjap.modules.lawenforcesuper.entity.ApDeptExtractInfoEntity;
import com.querydsl.core.QueryResults;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 安评单位Controller
 *
 * @author liyunyue
 */
@RestController
@RequestMapping("/apunit")
@PropertySource(value = "classpath:base.properties")
public class ApunitController {

    @Autowired
    private ApunitService apunitService;
    @Autowired
    private EnterpriseService enterpriseService;
    @Value("${BASE_URL}")
    private String BASE_URL;
    @Value("${CHECK_USER}")
    private String CHECK_USER;

    @Autowired
    private SysAuditService sysAuditService;
    @Autowired
    UserInfoService userInfoService;
    @Autowired
    HttpServletRequest request;

    /**
     * 安评单位注册
     *
     * @param apUnitEntity
     * @return
     */
    @PostMapping("/regist")
    public R regist(@RequestBody ApUnitEntity apUnitEntity, HttpServletRequest request) {
        String s;
        try {
            if (apUnitEntity.getUniscid() == null) {
                return R.error("统一社会信用代码不能为空");
            }
            // 判断该单位是否在黑名单中
            ApEnterpriseCreditEntity tyReConEnterPrise = enterpriseService.getTyReConEnterPrise("0", apUnitEntity.getUniscid());
            if (tyReConEnterPrise != null) {
                return R.error("该安评单位已存在于黑名单中，无法注册!");
            }
            if (apUnitEntity.getUserName() == null) {
                return R.error("用户名不能为空");
            }
            // 判断统一社会信用代码是否已存在
            boolean isExistEntCode = apunitService.isExistEntCode(apUnitEntity.getUniscid());
            if (isExistEntCode) {
                return R.error("统一信用代码已存在!");
            }
            // 判断用户名是否已存在
            boolean isExistUserName = apunitService.isExistUserName(apUnitEntity.getUserName());
            if (isExistUserName) {
                return R.error("用户名已存在!");
            }
            /*String url=BASE_URL+CHECK_USER;
            String msg= HttpClientUtil.doGet(url+apUnitEntity.getUserName());*/
            //boolean flag = baseService.checkUserExist(apUnitEntity.getUserName());
            boolean flag = userInfoService.checkUserExist(apUnitEntity.getUserName(), request);
            if (flag) {
                return R.error("用户名已存在");
            } else {
                apunitService.regist(apUnitEntity, request);
                s = JSON.toJSONString(apUnitEntity);
                sysAuditService.saveSuccessAudit("安评单位管理", Constant.APUNIT,Constant.PT,Constant.ADD,s,request);
                return R.ok();
            }

        } catch (Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(apUnitEntity);
            sysAuditService.saveFailAudit("安评单位管理",Constant.PUNISH,Constant.PT,Constant.PAGE,s,e,request);
            return R.error(e.getMessage());
        }
    }

    @PostMapping("/querySpResult")
    public R queryResult(@RequestParam String mobile,
                         @RequestParam(required = false) Integer pageNo,
                         @RequestParam(required = false) Integer pageSize) {
        String s="";
        try {
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<ApUnitEntity> apUnitEntityList = apunitService.queryByTel(pageRequest, mobile);
            s = JSON.toJSONString(apUnitEntityList);
            sysAuditService.saveSuccessAudit("安评单位管理", Constant.APUNIT,Constant.PT,Constant.PAGE,s,request);
            return R.ok().put("data", apUnitEntityList);
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("安评单位管理",Constant.PUNISH,Constant.PT,Constant.PAGE,s,e,request);
            return R.error(e.getMessage());
        }


    }


    @GetMapping("/queryById/{id}")
    public R queryById(@PathVariable String id) {
        String s;
        try {
            ApUnitEntity apUnitEntity = apunitService.queryById(id);
            s = JSON.toJSONString(apUnitEntity);
            sysAuditService.saveSuccessAudit("安评单位管理", Constant.APUNIT,Constant.PT,Constant.SELETE,s,request);
            return R.ok().put("data", apUnitEntity);
        }catch (Exception e){
            e.printStackTrace();
            s = JSON.toJSONString(id);
            sysAuditService.saveFailAudit("安评单位管理",Constant.PUNISH,Constant.PT,Constant.SELETE,s,e,request);
            return R.error(e.getMessage());
        }


    }
    /**
     * 交换系统返回检查对象List
     */
    @GetMapping("/queryAllApUnitMsg")
    public List queryAllJcdx() {
        String s = "";
        try {
            List<ApUnitEntity> list = apunitService.queryAllApUnitMsg();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
